package com.lyl.serviceresume.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lyl.serviceresume.entity.ResumeModel;
import com.lyl.serviceresume.mapper.ResumeModelMapper;
import com.lyl.serviceresume.service.ResumeModelService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 前台简历模板 服务实现类
 * </p>
 *
 * @author 雷艳玲
 * @since 2022-07-24
 */
@Service
public class ResumeModelServiceImpl extends ServiceImpl<ResumeModelMapper, ResumeModel> implements ResumeModelService {

    @Override
    public void pageQuery(Page<ResumeModel> pageParam, ResumeModel query) {
        QueryWrapper<ResumeModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("gmt_create");

        if (query == null){
            baseMapper.selectPage(pageParam, queryWrapper);
            return;
        }

        Long typeId = query.getTypeId();
        String title = query.getTitle();
//
        if (!StringUtils.isEmpty(title)) {
            queryWrapper.like("title", title);
        }
        if (!StringUtils.isEmpty(typeId) ) {
            queryWrapper.eq("type_id", typeId);
        }
        baseMapper.selectPage(pageParam, queryWrapper);
    }

    @Override
    public Map<String, Object> pageListWeb(Page<ResumeModel> pageParam, ResumeModel query) {

        QueryWrapper<ResumeModel> queryWrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(query.getTypeId())) {
            queryWrapper.eq("type_id", query.getTypeId());
        }


        baseMapper.selectPage(pageParam, queryWrapper);

        List<ResumeModel> records = pageParam.getRecords();
        long current = pageParam.getCurrent();//当前页
        long pages = pageParam.getPages();
        long size = pageParam.getSize();
        long total = pageParam.getTotal();
        boolean hasNext = pageParam.hasNext();
        boolean hasPrevious = pageParam.hasPrevious();

        Map<String, Object> map = new HashMap<String, Object>();
        map.put("items", records);
        map.put("current", current);
        map.put("pages", pages);
        map.put("size", size);
        map.put("total", total);
        map.put("hasNext", hasNext);
        map.put("hasPrevious", hasPrevious);

        return map;
    }
}
